package org.jeecg.modules.system.model;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.model.BaseImportModel;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.ImportExcelUtil;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysDictItem;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysDictItemService;
import org.jeecg.modules.system.service.impl.SysDictItemServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * 部门导入
 * @author mzz
 */
@Component
public class SysDepartImportModel extends BaseImportModel<SysDepart> {

    @Autowired
    private ISysDepartService sysDepartService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ISysDictItemService sysDictItemService;

    /**
     * 获取导入模板文件
     * @return
     */
    protected String getTemplateFilePath(){
//        return ClassUtils.getDefaultClassLoader().getResource("excelTemplate/import/sysDepart.xls").getPath();
        return "excelTemplate/import/sysDepart.xlsx";
    }

    /**
     * 保存导入的数据
     */
    @Override
    public void saveData(SysDepart sysDepart){
        String parentCode = sysDepart.getParentCode();
        LambdaQueryWrapper<SysDepart> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(SysDepart::getOrgCode, parentCode);
        SysDepart parent = sysDepartService.getOne(wrapper);
        if (parent == null) {
            throw new JeecgBootException("上级部门不存在");
        }
        sysDepart.setParentId(parent.getId());
        if(!StringUtils.isEmpty(sysDepart.getOrgCategory())){
            List<SysDictItem> sysDictItems = sysDictItemService.selectItemsByDictCodeAndItemText("org_category", sysDepart.getOrgCategory());
            if(!CollectionUtils.isEmpty(sysDictItems)){
                sysDepart.setOrgCategory(sysDictItems.get(0).getItemValue());
            }else{
                sysDepart.setOrgCategory("");
            }
        }
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        sysDepartService.saveDepartData(sysDepart, sysUser.getUsername());
    }


    @Override
    protected void preImport(List<SysDepart> dataList) {
        //按长度排序
        Collections.sort(dataList, new Comparator<SysDepart>() {
            @Override
            public int compare(SysDepart arg0, SysDepart arg1) {
                return arg0.getOrgCode().length() - arg1.getOrgCode().length();
            }
        });
    }

    @Override
    protected void afterImport(List<SysDepart> dataList) {
        //清空部门缓存
        Set keys3 = redisTemplate.keys(CacheConstant.SYS_DEPARTS_CACHE + "*");
        Set keys4 = redisTemplate.keys(CacheConstant.SYS_DEPART_IDS_CACHE + "*");
        redisTemplate.delete(keys3);
        redisTemplate.delete(keys4);
    }

    /**
     * 导入模板名称
     * @return
     */
    @Override
    public String getDemoFileName(){
        return "部门导入模板";
    }

}
